package com.fact.actions.ajax;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.actions.BaseAction;
import com.fact.models.NodeHierarchyService;
import com.fact.models.UserService;



public class AjaxAction extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	String response="";
	private String userId;
	private int segmentId;
	private int parentNode;
	
	public String validateUserId()
	{
		UserService userService=new UserService();
		HttpServletResponse response = ServletActionContext.getResponse();
		boolean res=userService.validateUserId(this.userId);	
		String responseString="NotFound";
		if(res==true)
			responseString="Available";
		try{
        	response.setContentType("text/xml");
        	response.setHeader("Cache-Control", "no-cache");
        	response.getWriter().write(responseString);
        	response.flushBuffer();
          }
        catch(Exception e) {e.printStackTrace();}
        
		return null;
	}
	
	public String nodeListString()
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		NodeHierarchyService nhService=new NodeHierarchyService();
		String nodeListString=nhService.getNodeListString(segmentId);
		try{
        	response.setContentType("text/xml");
        	response.setHeader("Cache-Control", "no-cache");
        	response.getWriter().write(nodeListString);
        	response.flushBuffer();
          }
        catch(Exception e) {e.printStackTrace();}
        
		return null;
	}
	public String parentNodeListString()
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		NodeHierarchyService nhService=new NodeHierarchyService();
		String nodeListString=nhService.getParentNodeListString(segmentId,parentNode);
		try{
        	response.setContentType("text/xml");
        	response.setHeader("Cache-Control", "no-cache");
        	response.getWriter().write(nodeListString);
        	response.flushBuffer();
          }
        catch(Exception e) {e.printStackTrace();}
        
		return null;
	}
	
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}

	public int getSegmentId() {
		return segmentId;
	}

	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}

	public int getParentNode() {
		return parentNode;
	}

	public void setParentNode(int parentNode) {
		this.parentNode = parentNode;
	}
	
	

}
